package com.we.connect.weconnect.controller.system;

import com.we.connect.weconnect.model.common.ResultModel;
import com.we.connect.weconnect.model.system.SysInfo;
import com.we.connect.weconnect.model.system.SysUser;
import com.we.connect.weconnect.model.system.vo.PasswordVo;
import com.we.connect.weconnect.service.SysInfoService;
import com.we.connect.weconnect.service.SysUserService;
import com.we.connect.weconnect.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;


@Controller
@RequestMapping("/system")
public class SysInfoController {

    @Autowired
    private SysInfoService sysInfoService;

    @Autowired
    private SysUserService sysUserService;

    @PostMapping("/addSysInfo")
    public String addSysInfo(@ModelAttribute("sysInfo") SysInfo sysInfo, Model model, HttpServletRequest request){
        SysUser loginSysUser = (SysUser)UserUtils.getSysUser(request, "loginSysUser");
        sysInfo.setUserId(loginSysUser.getUserId());
        Boolean isAddSuccess = sysInfoService.addSysInfo(sysInfo);
        if(isAddSuccess){
            return "redirect:/sysInfo";
        }
        model = ResultModel.fail("保存", "系统管理", "/sysInfo", model);
        return "common/tip";
    }

    @PostMapping("/updatePassword")
    public String updatePassword(@ModelAttribute("password") PasswordVo passwordVo, Model model, HttpServletRequest request){
        SysUser loginSysUser = (SysUser)UserUtils.getSysUser(request, "loginSysUser");
        Boolean isUpdateSuccess = sysUserService.updatePassword(passwordVo, loginSysUser);
        if(isUpdateSuccess){
            model = ResultModel.success("修改成功", "登录", "/login", model);
            UserUtils.delSysUser(request, "loginSysUser");
        }else {
            model = ResultModel.fail("修改失败", "修改", "/info", model);
        }
        return "common/tip";
    }


}
